﻿using System;
using System.IO;
using System.Net;
using HtmlAgilityPack;

namespace HttpCrawler.Engine
{
    public class HtmlHttpCrawlerContent : HttpCrawlerContent
    {
        /// <summary>
        /// Automatically detect encoding for HTML document. Default is false.
        /// </summary>
        public static bool AutoDetectEncoding { get; set; }

        public HtmlHttpCrawlerContent(Uri requestUrl, Uri referrerUrl, HttpWebResponse webResponse)
            : base(requestUrl, referrerUrl, webResponse)
        {
            Html = new HtmlDocument();
            Html.Load(Stream, AutoDetectEncoding);
        }

        public override void Save(Stream stream)
        {
            Html.Save(stream);
        }

        public HtmlDocument Html
        {
            get;
            protected set;
        }

        public override string Text
        {
            get
            {
                return Html.DocumentNode.OuterHtml;
            }
        }
    }
}
